package com.happy.study.servier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.study.dao.CollectionMapper;
import com.happy.study.entity.CollectionEntity;
import com.happy.study.servier.ICollectionService;
import com.happy.study.util.StateEnum;
import com.happy.study.util.StateException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, CollectionEntity> implements ICollectionService {

    @Override
    public List<CollectionEntity> queryList ( CollectionEntity collectionEntity ) {
        QueryWrapper<CollectionEntity> queryWrapper = new QueryWrapper<> ();
        if ( collectionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( collectionEntity.getUserId () != null ) {
            queryWrapper.eq ( "userId", collectionEntity.getUserId () );
        }
        List<CollectionEntity> collectionEntities = baseMapper.selectList ( queryWrapper );
        return collectionEntities;
    }

    @Override
    public CollectionEntity querySingle ( CollectionEntity collectionEntity ) {
        if ( collectionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( collectionEntity.getId () != null ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        collectionEntity = baseMapper.selectById ( collectionEntity );
        return collectionEntity;
    }

    @Override
    public CollectionEntity addSingle ( CollectionEntity collectionEntity ) {
        if ( collectionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( collectionEntity.getRelationId () == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( collectionEntity.getUserId () == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        collectionEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( collectionEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return collectionEntity;
    }
}
